
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class Conn {
	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	private Connection connect = null;
	private Statement statement = null;
	private Connection conn = null;
	private ResultSet resultSet = null;
	private int val_noua=0;
	private int contor1=0;
	private int contor2=0;
	public List<String> date=new ArrayList<String>();
	public List<Integer> valori=new ArrayList<Integer>();
	//private PreparedStatement preparedStatement = null;
	public void conectare(){
		 try
         {
             String userName = "root";
             String password = "gravitant";
             String url = "jdbc:mysql://localhost/date";
             Class.forName ("com.mysql.jdbc.Driver").newInstance ();
             conn = DriverManager.getConnection (url, userName, password);
             System.out.println ("Database connection established");
         }
         catch (Exception e)
         {
             System.err.println ("Cannot connect to database server");
         }
	}
	
	public void deconectare(){
		 try
        {

            conn.close();

        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
	}
	
	public void IntoarceDate(){
		List<String> datetemp=new ArrayList<String>();
		Statement s;
		try {
		s = conn.createStatement();
		s.executeQuery ("select * from (select * from date_receptionate order by id desc limit 10)as aa order by id;");
        ResultSet rs = s.getResultSet();
        int count = 0;
        while (rs.next ())
        {
            int idVal = rs.getInt ("id");
            //int nameVal = rs.getInt("valoare");
            String catVal = rs.getString ("data");
            System.out.println (
                    "id = " + idVal
              //      + ", valoare = " + nameVal
                    + ", data = " + catVal);
            ++count;
            if(contor1<count){
                contor1=count;
            	date.add(catVal);
            }
        }
        rs.close ();
        s.close ();
        System.out.println (count + " rows were retrieved");
		}
		catch(SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return date;
	}
	
	public void IntoarceValori(){
		List<Integer> valoritemp=new ArrayList<Integer>();
		Statement s;
		try {
		s = conn.createStatement();
		s.executeQuery ("select * from (select * from date_receptionate order by id desc limit 10)as aa order by id;");
        ResultSet rs = s.getResultSet ();
        int count = 0;
        while (rs.next ())
        {
            int idVal = rs.getInt ("id");
            int nameVal = rs.getInt("valoare");
            //String catVal = rs.getString ("data");
            System.out.println (
                    "id = " + idVal
                    + ", valoare = " + nameVal);
            //        + ", data = " + catVal);
            ++count;
            if(contor2<count){
                contor2=count;
            valori.add(nameVal);
            }
        }
        rs.close ();
        s.close ();
        System.out.println (count + " rows were retrieved");
		}
		catch(SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	return valoritemp;
	}
	public void Inserare(int valoare){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
 	    String data=sdf.format(cal.getTime());
		Statement s;
		try {
			s = conn.createStatement();
    		s.executeUpdate ("INSERT INTO date_receptionate (valoare, data)"+ "VALUES ("+ val_noua + ",'" + data + "')");
		    s.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
   }
	
	       public static void main (String[] args)
	       {
			int i=0;
			int val[] = new int[7];
	    	   Conn conectare=new Conn();
			val[1]=1;
			val[2]=2;
			val[3]=2;
			val[4]=5;
			val[5]=6;		
			
			conectare.conectare();
			  while(true){
				  i++;
			  if(i==0)
			  {	 
				  conectare.val_noua=0;
			  }
			  else if(i==3)
			  {
				  conectare.val_noua+=val[2];
			  }
			  else if(i==9)
			  {
				  conectare.val_noua+=val[5];
			  }
			  else if(i==16)
			  {
			      conectare.val_noua+=val[3];
			  }
			  else if(i==18)
			  {
				  conectare.val_noua+=val[1];
			  }
			  else if(i==20)
			  {
				  conectare.val_noua-=val[3];
			  }
			  else if(i==22)
			  {
				  conectare.val_noua-=val[5];
			  }
			  else if(i==26)
			  {
				  conectare.val_noua+=val[5];
			  }
			  else if(i==28)
			  {
				  conectare.val_noua-=val[5];
			  }
			  else if(i==29)
			  {
				  conectare.val_noua-=val[2];
			  }
			  else if(i==30)
			  {
			      i=0;
			  }
        	  conectare.Inserare(conectare.val_noua);
			  //conectare.IntoarceDate();
			  //conectare.IntoarceValori();
			  try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			  
			  
			  
			  /*  List<String> date=new ArrayList<String>();  
			  List<Integer> valori=new ArrayList<Integer>(); 
			         
			 
			  Connection conn = null;
              int val_noua=2;
              int idx;
	           try
	           {
	               String userName = "root";
	               String password = "1234";
	               String url = "jdbc:mysql://localhost/date";
	               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
	               conn = DriverManager.getConnection (url, userName, password);
	               System.out.println ("Database connection established");
	               
	               
	               int i=0;
	               while(i<10){
	            	   i++;
	            	   int contor=0;
	            	   Calendar cal = Calendar.getInstance();
	   			       SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	            	   String data=sdf.format(cal.getTime());
	            	   val_noua=val_noua+2;
	            	   Statement s = conn.createStatement ();
	               idx=s.executeUpdate (
	       		   "INSERT INTO date_receptionate (valoare, data)"
	            		                       + "VALUES ("+ val_noua + ",'" + data + "')");
	               System.out.printf(data);
	               s.executeQuery ("SELECT * FROM date_receptionate");
	               ResultSet rs = s.getResultSet ();
	               int count = 0;
	               while (rs.next ())
	               {
	                   int idVal = rs.getInt ("id");
	                   int nameVal = rs.getInt("valoare");
	                   String catVal = rs.getString ("data");
	                   System.out.println (
	                           "id = " + idVal
	                           + ", valoare = " + nameVal
	                           + ", data = " + catVal);
	                   ++count;
	                   
	                   if(count>contor){
	                	   contor=count;
		                   valori.add(nameVal); 
		                   date.add(catVal);
	                   }
         
	                                   
	               }
	               rs.close ();
	               s.close ();
	               System.out.println (count + " rows were retrieved");
	           
	               Thread.sleep(6000);
	               }
	           
	           }
	           catch (Exception e)
	           {
	               System.err.println ("Cannot connect to database server");
	           }
	           finally
	           {
	               if (conn != null)
	               {
	                   try
	                   {
	                       conn.close ();
	                       System.out.println ("Database connection terminated");
	                   }
	                   catch (Exception e) { /* ignore close errors */ }
	        //       }
	        //   }
	       //return date;
	       //}

}
